<template>
  <div class="sbody">
    <headMain :mensid="mensid" />
    <!-- 考核列表 -->
    <div class="tplistbox">
      <div class="bjbox">
        <img src="@/assets/images/bjt.png" alt="" />
      </div>
      <div class="ztnr">
        <div
          class="exramitembox"
          v-for="item in eramdata"
          :key="item.examineId"
        >
          <div class="exramtop">
            <div>
              <img src="@/assets/images/khicon.png" alt="" />
            </div>
            <div class="eramnamebox">{{ item.examineName }}</div>
            <div class="eramnclass">
              考核范围:
              <span>{{ item.organizationName }}</span>
            </div>
          </div>
          <div class="exrambottom">
            <!-- <div class="exramtime">
              <div class="exramtimeleft">考核范围:</div>
              <div class="exramtimeright">{{ item.organizationName }}</div>
            </div> -->
            <div class="exramtime">
              <div class="exramtimeleft">开始时间:</div>
              <div class="exramtimeright">{{ item.startTime }}</div>
            </div>
            <div class="exramtime">
              <div class="exramtimeleft">结束时间:</div>
              <div class="exramtimeright">{{ item.endTime }}</div>
            </div>
            <div class="exramtime">
              <div class="exramtimeleft">考核时限:</div>
              <div class="exramtimeright">{{ item.examineTime }}分钟</div>
            </div>
            <div
              class="exramstartbox"
              @click="choicefn(item)"
              :class="choicefnbj(item) ? 'exramstartbox' : 'exramstartboxs'"
              >参加考核</div
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 选择考核试题 -->
    <div class="tcbox" v-if="ksshow">
      <div class="tctopbox">
        <div class="tctopname">考核详细信息</div>
        <div class="tcclose" @click="ksshow = false"
          ><icon-close :size="25"
        /></div>
      </div>
      <div class="tcmainbox">
        <div class="tcmainitem">
          <!-- {{khitem}} -->
          <div class="tcitemname">考核名称</div>
          <div class="tcitdes">{{ khitem.examineName }}</div>
        </div>
        <div class="tcmainitem">
          <div class="tcitemname">组织教师</div>
          <div class="tcitdes">{{ khitem.examineName }}</div>
        </div>
        <div class="tcmainitem">
          <div class="tcitemname">时限</div>
          <div class="tcitdes">{{ khitem.examineTime }}分钟</div>
        </div>
        <div class="tcmainitem">
          <div class="tcitemname">时间</div>
          <div class="tcitdes"
            >{{ khitem.startTime }} ~ {{ khitem.endTime }}</div
          >
        </div>
        <div class="tcmainitem">
          <div class="tcitemname">选择图谱</div>
          <div class="tcitdes">
            <a-select
              :options="options"
              placeholder="请选择图谱"
              :field-names="fieldNames"
              :style="{ width: '100%', height: '100%' }"
              @change="changefn"
              v-model="medicineAtlasId"
            />
          </div>
        </div>
      </div>
      <div class="tsred" v-if="tipshow">请先选择图谱才能进行作答</div>
      <div class="startbox" @click="gostart">开始作答</div>
    </div>
    <div class="zzcbox" v-if="ksshow"></div>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, reactive } from 'vue';
  import { Message, Modal } from '@arco-design/web-vue';
  import headMain from '@/views/headmain/index.vue';
  import { khList, addJL, getTp, getTplist } from '@/api/examine.ts';
  import { getUser } from '@/api/tplist.ts';
  import { useRouter } from 'vue-router';
  const router = useRouter();
  const mensid = ref('2');
  const eramdata = ref([]);
  const khListh = () => {
    // 查询学生信息
    getUser().then((res) => {
      if (res.code == 200) {
        console.log('查询学生信息完成', res.data);
        studentId.value = res.data.studentId;
        khList({
          organizationId: res.data.organizationId,
          studentId: res.data.studentId,
        }).then((res) => {
          if (res.code == 200) {
            eramdata.value = res.data;
            console.log('查询列表成功', res.data);
          }
        });
      }
    });
  };
  const tipshow = ref(false);
  // 学生id
  const studentId = ref('');
  // 考核id
  const examineId = ref('');
  // 考核记录ID
  const examineRecordId = ref('');
  // 考核时限
  const examineTime = ref('');
  // 定义当前选择考核的图谱
  const medicineAtlasId = ref('');
  // 考核开始 时间
  const startTime = ref('');
  // 考核信息暂存
  // 选择考核信息
  const fieldNames = {
    value: 'medicineAtlasId',
    label: 'medicineAtlasName',
  };
  const options = ref([]);
  const choicefnbj = (item) => {
    const inputTime = new Date(item.startTime); // 传入的时间
    const currentTime = new Date(); // 当前时间

    return currentTime >= inputTime;
  };
  const choicefn = (item) => {
    const inputTime = new Date(item.startTime); // 传入的时间
    const currentTime = new Date(); // 当前时间

    const isEqualOrAfter = currentTime >= inputTime;
    console.log('比较时间测试', isEqualOrAfter);

    if (isEqualOrAfter) {
      ksshow.value = true;
      khitem.value = item;
      examineId.value = item.examineId;
      examineTime.value = item.examineTime;
      startTime.value = item.startTime;
      getTp({
        examineId: examineId.value,
      }).then((res) => {
        if (res.code == 200) {
          console.log('查询考核图谱列表成功', res.data);
          options.value = res.data;
        }
      });
    }
  };
  const khitem = ref('');
  const gostart = () => {
    if (medicineAtlasId.value) {
      addJL({ studentId: studentId.value, examineId: examineId.value }).then(
        (res) => {
          if (res.code == 200) {
            console.log('添加考核记录成功', res.data);
            examineRecordId.value = res.data;
            tipshow.value = false;
            router.push({
              name: 'exram',
              query: {
                examineId: examineId.value,
                studentId: studentId.value,
                examineRecordId: examineRecordId.value,
                // examineTime: examineTime.value,
                medicineAtlasId: medicineAtlasId.value,
                khitem: JSON.stringify(khitem.value), // Convert to JSON string
              },
            });
          }
        }
      );
    } else {
      tipshow.value = true;
    }
  };
  khListh();
  // 选择考试
  const ksshow = ref(false);
  const changefn = () => {
    tipshow.value = false;
    console.log('当前选择的考核图谱id', medicineAtlasId.value);
  };
</script>

<style scoped>
  .tplistbox {
    display: flex;
    flex-direction: column;
    /* margin: 0 auto; */
    /* margin-top: -150px; */
    position: relative;
    height: auto;
  }
  .bjbox {
    width: 100%;
    height: 172px;
    position: absolute;
    top: 0;
  }
  .bjbox img {
    width: 100%;
    height: 172px;
  }
  .ztnr {
    width: 1200px;
    z-index: 99;
    margin: 20px auto;
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    justify-content: space-between; /* 在行中均匀分布 */
  }

  .exramitembox {
    width: calc(50% - 10px); /* 每个盒子占据一半宽度，减去间距 */
    height: 347px;
    background: #f5f6fa;
    box-shadow: 0px 4px 11px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    margin-bottom: 20px; /* 下方间距 */
    display: flex;
    flex-direction: column;
  }
  .exramtop {
    width: 593px;
    height: 140px;
    background: #ffffff;
    border-radius: 4px 4px 0px 0px;
    display: flex;
    flex-direction: column;
    padding: 12px;
  }
  .exrambottom {
    width: 593px;
    height: 207px;
    background: #f5f6fa;
    box-shadow: 0px 4px 11px 0px rgba(0, 0, 0, 0.1);
  }
  .eramnamebox {
    font-size: 18px;
    color: #000000;
    width: 555px;
    font-weight: bold;
    overflow: hidden;
    text-wrap: nowrap;
    text-overflow: ellipsis;
    margin: 15px 0;
  }
  .eramnclass {
    font-size: 16px;
    color: #999999;
  }
  .eramnclass span {
    color: #333333;
  }
  .exramstartbox {
    width: 106px;
    height: 34px;
    background: #0c71ff;
    border-radius: 4px 4px 4px 4px;
    color: #ffffff;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    cursor: pointer;
  }
  .exramstartboxs {
    background: rgba(12, 113, 255, 0.5);
  }
  .exrambottom {
    display: flex;
    flex-direction: column;
    padding: 0 12px;
  }
  .exramtime {
    margin: 15px 0;
    display: flex;
    justify-content: space-between;
  }
  .exramtimeleft {
    font-size: 16px;
    color: #999999;
  }
  .exramtimeright {
    font-size: 16px;
    color: #333333;
  }
  .tcbox {
    width: 713px;
    height: 429px;
    background: #ffffff;
    border-radius: 0px 0px 0px 0px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 999;
    display: flex;
    flex-direction: column;
  }
  .tctopbox {
    width: 100%;
    height: 46px;
    background: #cfcfcf;
    display: flex;
    align-items: center;
  }
  .tcclose {
    position: absolute;
    right: 10px;
    cursor: pointer;
  }
  .tctopname {
    font-size: 16px;
    color: #000000;
    margin: 0 auto;
  }
  .tcmainbox {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 0;
  }
  .tcmainitem {
    display: flex;
    align-items: center;
    margin: 0 auto;
    border: 1px solid #c1c1c1;
    width: 491px;
    height: 44px;
  }
  .tcitemname {
    width: 175px;
    height: 44px;
    display: flex;
    font-size: 16px;
    align-items: center;
    justify-content: center;
    border-right: 1px solid #c1c1c1;
  }
  .tcitdes {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
  }
  .zzcbox {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 99;
    position: absolute;
    top: 0;
    left: 0;
  }
  .startbox {
    width: 112px;
    height: 37px;
    background: linear-gradient(180deg, #ffffff 0%, #c0c0c0 100%);
    border-radius: 4px 4px 4px 4px;
    border: 1px solid #999999;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    cursor: pointer;
    font-size: 16px;
  }
  .tsred {
    color: red;

    position: absolute;
    bottom: 95px;
    left: 50%;
    transform: translate(-50%, 0);
  }
</style>
